#include <string.h>
#include <stdint.h>
#include <sdcc-lib.h>

#ifndef _SDCC_PORT_PROVIDES_MEMMOVE
#define _SDCC_PORT_PROVIDES_MEMMOVE 0
#endif

#if !_SDCC_PORT_PROVIDES_MEMMOVE

void * memmove( void * dst, const void * src, size_t acount ) {
  void * ret = dst;
  char * d;
  const char * s;
  if( ( uintptr_t )src < ( uintptr_t )dst ) {
    /*
       copy from higher addresses to lower addresses
    */
    d = ( ( char * )dst ) + acount - 1;
    s = ( ( char * )src ) + acount - 1;
    while( acount-- ) {
      *d-- = *s--;
    }
  } else {
    /*
       copy from lower addresses to higher addresses
    */
    d = dst;
    s = src;
    while( acount-- ) {
      *d++ = *s++;
    }
  }
  return( ret );
}
#endif
